package com.jn.exam.oss.Face;

import com.alibaba.fastjson.JSONObject;
import com.jn.exam.oss.SetingModel.Setingmodel;
import com.jn.exam.oss.schedule.ScheduledTask;
import com.jn.exam.oss.utils.GetToken;
import com.jn.exam.oss.utils.GsonUtils;
import com.jn.exam.oss.utils.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
@RefreshScope
public class BaiduAIFace {


    @Value("${baiduyun.api.API_KEY}")
    private String api_key;

    @Value("${baiduyun.api.SECRET_KEY}")
    private String secret_key;

    @Autowired
    private ScheduledTask scheduledTask;

    /**
     * 人脸注册
     */
    public Map FaceRegistration(Setingmodel facaddseting) throws IOException {
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add";
        Map resultmap = FaceAddAndUpdate(facaddseting, url);
        return resultmap;
    }

    /**
     * 人脸更新
     *
     * @param facaddseting 参数设置
     * @return 返回信息map
     * @throws IOException
     */
    public Map FaceUpdate(Setingmodel facaddseting) throws IOException {
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/update";
        Map resultmap = FaceAddAndUpdate(facaddseting, url);
        return resultmap;
    }

    private Map FaceAddAndUpdate(Setingmodel facaddseting, String url) throws IOException {
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("image", facaddseting.getImgpath());
            map.put("group_id", facaddseting.getGroupID());
            map.put("user_id", facaddseting.getUserID());
            map.put("liveness_control", facaddseting.getLiveness_Control());
            map.put("image_type", facaddseting.getImage_Type());
            map.put("quality_control", facaddseting.getQuality_Control());
            String param = GsonUtils.toJson(map);
            String result = HttpUtil.post(url, scheduledTask.getAssess_token(), "application/json", param);
            Map resultmap = GsonUtils.fromJson(result, Map.class);
            return resultmap;
        } catch (Exception e) {
            System.out.println("失败");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询这个人的面部信息
     *
     * @param setingmodel
     * @return map
     */
    public Map FindPersonFaceList(Setingmodel setingmodel) {
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/face/getlist";
        Map<String, Object> map = new HashMap<>();
        if (!map.isEmpty()) {
            map.clear();
        }
        map.put("group_id", setingmodel.getGroupID());
        map.put("user_id", setingmodel.getUserID());
        String param = GsonUtils.toJson(map);

        try {
            String result = HttpUtil.post(url, scheduledTask.getAssess_token(), "application/json", param);
            Map resultmap = GsonUtils.fromJson(result, Map.class);
            return resultmap;
        } catch (Exception e) {
            System.out.println("查询失败");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询本组的面部信息
     *
     * @param setingmodel
     * @return
     */
    public Map FindGroupList(Setingmodel setingmodel) {
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/getusers";
        Map<String, Object> map = new HashMap<>();
        map.put("group_id", setingmodel.getGroupID());
        String param = GsonUtils.toJson(map);
        try {
            String result = HttpUtil.post(url, scheduledTask.getAssess_token(), "application/json", param);
            Map resultmap = GsonUtils.fromJson(result, Map.class);
            return resultmap;

        } catch (Exception e) {
            System.out.println("未查询到人脸信息");
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 删除人脸
     *
     * @param setingmodel
     * @return
     */
    public Map DelPersonFace(Setingmodel setingmodel) {
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/face/delete";
        Map map = FindPersonFaceList(setingmodel);
        Object result = map.get("result");
        String s = GsonUtils.toJson(result);
        JSONObject jsonObject = JSONObject.parseObject(s);
        String face_token = jsonObject.getString("face_list");
        String substring = face_token.substring(2, face_token.length() - 2);
        String[] split = substring.split("\"");
        face_token = split[7];
        System.out.println(face_token);
        map.put("group_id", setingmodel.getGroupID());
        map.put("user_id", setingmodel.getUserID());
        map.put("face_token", face_token);
        String param = GsonUtils.toJson(map);
        try {
            String result2 = HttpUtil.post(url, scheduledTask.getAssess_token(), "application/json", param);
            Map resultmap = GsonUtils.fromJson(result2, Map.class);
            return resultmap;
        } catch (Exception e) {
            System.out.println("删除失败");
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 用于将用户从某个组中删除
     * @param setingmodel
     * @return
     */
    public Map deleteUserFromGroup(Setingmodel setingmodel){
        String url="https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/delete";
        try {
            Map<String,Object> map=new HashMap<>();
            map.put("group_id",setingmodel.getGroupID());
            map.put("user_id",setingmodel.getUserID());
            String param=GsonUtils.toJson(map);
            String result=HttpUtil.post(url,scheduledTask.getAssess_token(), "application/json", param);
            Map<String, Object> resultmaps = GsonUtils.fromJson(result, Map.class);
            return resultmaps;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 人脸查找1:N
     *
     * @return
     */
    public Map FaceSearchOne(Setingmodel setingmodel) throws IOException {
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/search";
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("image", setingmodel.getImgpath());
            map.put("liveness_control", setingmodel.getLiveness_Control());
            map.put("group_id_list", setingmodel.getGroupID());
            map.put("user_id", setingmodel.getUserID());
            map.put("image_type", setingmodel.getImage_Type());
            map.put("quality_control", setingmodel.getQuality_Control());
            String param = GsonUtils.toJson(map);
            String result = HttpUtil.post(url, scheduledTask.getAssess_token(), "application/json", param);
            Map<String, Object> resultmaps = GsonUtils.fromJson(result, Map.class);
            return resultmaps;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 人脸查找M:N
     *
     * @param setingmodel
     * @return
     */
    public Map FaceSearchSome(Setingmodel setingmodel) {
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/multi-search";
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("image", setingmodel.getImgpath());
            map.put("liveness_control", setingmodel.getLiveness_Control());
            map.put("group_id_list", setingmodel.getGroupID());
            map.put("image_type", setingmodel.getImage_Type());
            map.put("max_face_num", setingmodel.getMax_Face_Num());
            map.put("quality_control", setingmodel.getQuality_Control());
            String param = GsonUtils.toJson(map);
            String result = HttpUtil.post(url, scheduledTask.getAssess_token(), "application/json", param);
            Map<String, Object> resultmaps = GsonUtils.fromJson(result, Map.class);
            return resultmaps;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //人脸检测
    public Map FaceCheck(Setingmodel setingmodel) {
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/detect";
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("image", setingmodel.getImgpath());
            map.put("image_type", setingmodel.getImage_Type());
            map.put("max_face_num", setingmodel.getMax_Face_Num());
            String param = GsonUtils.toJson(map);
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String result = HttpUtil.post(url, scheduledTask.getAssess_token(), "application/json", param);
            Map<String, Object> resultmaps = GsonUtils.fromJson(result, Map.class);
            return resultmaps;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
